﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XLib.Base.Ex;
using XLib.Base.VirtualDisk;

namespace XLib.Node
{
    public class NodeFactory
    {
        public static NodeFactory Ins { get; } = new NodeFactory();
        /// <summary>节点库磁盘</summary>
        protected Harddisk _nodeLibRoot = new Harddisk();
        public Harddisk LibHarddisk
        {
            get => _nodeLibRoot; set => _nodeLibRoot = value;
        }
        /// <summary>根文件夹</summary>
        public Folder Root => _nodeLibRoot.Root;

        //private Dictionary<PinGroupClassType, PinGroupViewType> pingroup2Views = new Dictionary<PinGroupClassType, PinGroupViewType>();
        private Dictionary<Type, PinGroupViewType> pingroupType2Views = new Dictionary<Type, PinGroupViewType>();

        /// <summary>
        /// 创建节点
        /// </summary>
        public virtual NodeBase? CreateNode(string typeString)
        {
            //NodeBase? result = _nodeLibRoot
            foreach (var file in _nodeLibRoot.FileDict)
            {
                if (file.Value.Name == typeString)
                {
                    NodeType nodeType = file.Value.Instance as NodeType;
                    return nodeType.NewInstance();
                }
            }
            return null;
        }

        /// <summary>
        /// 注册节点
        /// </summary>
        /// <typeparam name="TNode"></typeparam>
        /// <param name="libname"></param>
        /// <param name="top_group"></param>
        /// <param name="sub_group"></param>
        /// <returns></returns>
        public bool RegisterNode<TNode>(string libname, string top_group, string sub_group) where TNode : NodeBase, new()
        {
            NodeType nodeType = new NodeType<TNode>();
            XLib.Base.VirtualDisk.File? result_file = null;
            var rootfolder = _nodeLibRoot.FindFolder(new List<string> { libname });
            if (rootfolder == null)
            {
                rootfolder = _nodeLibRoot.CreateFolder(libname.PackToList());
            }
            var top_folder = rootfolder.FindFoder(top_group);
            if (top_folder == null)
            {
                top_folder = _nodeLibRoot.CreateFolder(rootfolder, top_group.PackToList());
            }
            if (sub_group == "")
            {
                result_file = _nodeLibRoot.CreateFile(top_folder, typeof(TNode).Name, "nt", nodeType);
            }
            else
            {
                var sub_folder = top_folder.FindFoder(sub_group);
                if (sub_folder == null)
                {
                    sub_folder = _nodeLibRoot.CreateFolder(top_folder, sub_group.PackToList());
                }
                result_file = _nodeLibRoot.CreateFile(sub_folder, typeof(TNode).Name, "nt", nodeType);
            }
            return true;
        }

        /// <summary>
        /// 注册pingroup和pingroupview的对应关系
        /// </summary>
        /// <typeparam name="TPinGroup"></typeparam>
        /// <typeparam name="TPinGroupView"></typeparam>
        /// <returns></returns>
        public bool RegisterPinGroupPair<TPinGroup, TPinGroupView>() where TPinGroup : PinGroupBase where TPinGroupView : PinGroupViewBase, new()
        {
            Type selftype = typeof(TPinGroup);
            PinGroupViewType<TPinGroupView> pinGroupViewType = new PinGroupViewType<TPinGroupView>();
            pingroupType2Views.Add(selftype, pinGroupViewType);
            return true;
        }

        public PinGroupViewBase? CreatePinGroupView(PinGroupBase pinGroupBase)
        {
            //获取pingroupbase 的父类
            var parenttype = pinGroupBase.GetType();
            if (parenttype == null)
            {
                return null;
            }
            if (pingroupType2Views.TryGetValue(parenttype, out var viewtype))
            {
                var viewbase = viewtype.NewInstance();
                viewbase.PinGroupInstance = pinGroupBase;
                return viewbase;
            }
            return null;
        }

    }
}
